#include "sms.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>


#define VIDEO_LIST_LAYOUT_NAME "layout-video-list"

typedef enum _SLISTVIEW_POSITION_ID{
	LISTVIEW_POSITION_ID_LEFT,
	LISTVIEW_POSITION_ID_RIGHT,
	LISTVIEW_POSITION_ID_COUNT
}SLISTVIEW_POSITION_ID;


typedef struct _Title{
	gdouble icon_x;
	gdouble icon_y;
	gdouble text_x;
	gdouble text_y;
	char *font;
	char *colour;
	GooCanvasItem *icon;
	GooCanvasItem *text;
}Title;

typedef struct _Page_Controller{
	gdouble page_up_x;
	gdouble page_up_y;
	gdouble text_x;
	gdouble text_y;
	gdouble page_down_x;
	gdouble page_down_y;
	GooCanvasItem *page_up;
	GooCanvasItem *page_down;
    GooCanvasItem *text;
}Page_Controller;

typedef struct _List_Text{
	GdkPixbuf *icon;
	char *dir;
	char *text;
	char *layout;
	char *layout_xml_path;
}List_Text;

typedef struct _VIDEO_LIST_LAYOUT{
	GooCanvasItem *bg;
	GooCanvasItem *right_list_bg;
	GooCanvasItem *progress_bar;
	GooCanvasItem *arrow;
	GooCanvasItem *icon;
	GooCanvasItem *icon_text;

	int selected_list;

	GooCanvasItem *text;
	Downloader *downloader;

	SListView *left_list_view;
	SListView *right_list_view;
	int left_list_text_sum;
	int right_list_text_sum;
  List_Text **left_list_text;
  List_Text **right_list_text;

  int right_list_cur_page;
  int right_list_total_page;

	Title title;
	Page_Controller page_controller;
}Video_List_Layout;

gboolean wait = FALSE;//this is a tag for all listview load finish

static gboolean video_list_layout_load_right_list(xmlNode *a_node, Layout *layout);
static gboolean video_list_layout_load_config(Layout *layout, char *conf_file, gboolean (* travel_xml)(xmlNode *, Layout *));


static gboolean video_list_layout_remove_right_list(Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;
	int i;

	slistview_clear_list(video_list_layout->right_list_view);
	for(i=0; i<video_list_layout->right_list_text_sum; i++) {
		free(video_list_layout->right_list_text[i]->layout);
		free(video_list_layout->right_list_text[i]->layout_xml_path);
		free(video_list_layout->right_list_text[i]->text);
	}
	free(video_list_layout->right_list_text);
}

static gboolean video_list_layout_load_bg(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;
	GdkPixbuf*  pixbuf_bg, *pixbuf_right_list_bg;
	xmlChar * val;
	gdouble right_list_bg_x, right_list_bg_y;
	char pic[256];

	if(video_list_layout->bg||!a_node)
		return FALSE;

	val = xmlGetProp (a_node,(xmlChar*)"pic");
	if(!val)
		return FALSE;

	sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
	printf("pic=%s\n" , pic);
	pixbuf_bg = sms_load_pic(pic,800,480);
	if(!pixbuf_bg)
		return FALSE;

	video_list_layout->bg=goo_canvas_image_new (layout->cavas, pixbuf_bg, 0, 0, NULL);

	right_list_bg_x = sms_get_xml_double (a_node, (xmlChar*)"right_list_bg_x", 0);
	right_list_bg_y = sms_get_xml_double (a_node, (xmlChar*)"right_list_bg_y", 0);

	val = xmlGetProp (a_node,(xmlChar*)"right_list_bg");
	if(!val)
		return FALSE;
	sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
	printf("pic=%s\n", pic);
	pixbuf_right_list_bg = gdk_pixbuf_new_from_file(pic, NULL);
	video_list_layout->right_list_bg = goo_canvas_image_new (layout->cavas, pixbuf_right_list_bg, right_list_bg_x, right_list_bg_y, NULL);

	g_object_unref (G_OBJECT (pixbuf_bg));
	g_object_unref (G_OBJECT (pixbuf_right_list_bg));
	printf("finish func %s\n" ,__FUNCTION__);

	return TRUE;

}

static gboolean video_list_layout_load_title(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);

	if(!a_node)
		return FALSE;

	Video_List_Layout *video_list_layout = layout->private;
	GdkPixbuf*  pixbuf;
	xmlChar *val;
	char pic[256];

	video_list_layout->title.icon_x = sms_get_xml_double (a_node, (xmlChar*)"icon_x", 0);
	video_list_layout->title.icon_y = sms_get_xml_double (a_node, (xmlChar*)"icon_y", 0);
	video_list_layout->title.text_x = sms_get_xml_double (a_node, (xmlChar*)"text_x", 0);
	video_list_layout->title.text_y = sms_get_xml_double (a_node, (xmlChar*)"text_y", 0);

	val = xmlGetProp (a_node, (xmlChar*)"colour");


	if(!val) {
		//colour = "black";
		video_list_layout->title.colour = "black";
	} else {
		video_list_layout->title.colour = val;
	}

	val = xmlGetProp (a_node, (xmlChar*)"icon");
	if(!val)
	   	return FALSE;
	sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);

	pixbuf = gdk_pixbuf_new_from_file(pic, NULL);
	if(!pixbuf)
	  	return FALSE;
	video_list_layout->title.icon = goo_canvas_image_new (layout->cavas, pixbuf, video_list_layout->title.icon_x,
			                                                                     video_list_layout->title.icon_y, NULL);
	g_object_unref (G_OBJECT (pixbuf));

	val = xmlGetProp (a_node, (xmlChar*)"font");
	if(!val) {
		//font = "Sans 20";
		video_list_layout->title.font = "Sans 20";
	} else {
		video_list_layout->title.font = val;
	}

	val = xmlGetProp (a_node, (xmlChar*)"text");

	video_list_layout->title.text = goo_canvas_text_new(layout->cavas, val, video_list_layout->title.text_x,
			                                                                video_list_layout->title.text_y, 120, GTK_ANCHOR_NW,
			                                                                  "font", video_list_layout->title.font,//introduction_layout->title.font,
			                                                                  "fill-color", video_list_layout->title.colour,
			                                                                  "ellipsize", PANGO_ELLIPSIZE_END,
			                                                                  "alignment", PANGO_ALIGN_LEFT, NULL);

	return TRUE;
}

static gboolean video_list_layout_show_page_controller(Layout *layout, gboolean isShowUpBtn, gboolean isShowDownBtn, int page)
{
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;
	char buf[10];

	if (isShowUpBtn) {
		g_object_set(video_list_layout->page_controller.page_up, "visibility", GOO_CANVAS_ITEM_VISIBLE, NULL);
	} else {
		g_object_set(video_list_layout->page_controller.page_up, "visibility", GOO_CANVAS_ITEM_HIDDEN, NULL);
	}

	if (isShowDownBtn) {
		g_object_set(video_list_layout->page_controller.page_down, "visibility", GOO_CANVAS_ITEM_VISIBLE, NULL);
	} else {
		g_object_set(video_list_layout->page_controller.page_down, "visibility", GOO_CANVAS_ITEM_HIDDEN, NULL);
	}

	if (page > 0) {
		sprintf(buf, "第%d页", page);
		g_object_set(video_list_layout->page_controller.text, "text", buf, NULL);
	}

	return TRUE;
}

static gboolean video_list_layout_load_page_controller(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);

	if(!a_node)
		return FALSE;
	xmlNode *cur_node = NULL;
	GdkPixbuf *pixbuf_page_up, *pixbuf_page_down;
	Video_List_Layout *video_list_layout = layout->private;
	xmlChar *val;
	xmlChar *font;
	xmlChar *colour;

	int width, height;
	char pic[256];

	for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			printf("node type: Element, name: %s\n", cur_node->name);
			if(strcmp(cur_node->name, "page-up-btn")==0) {
				video_list_layout->page_controller.page_up_x = sms_get_xml_double (cur_node, (xmlChar*)"x", 0);
				video_list_layout->page_controller.page_up_y = sms_get_xml_double (cur_node, (xmlChar*)"y", 0);
                width = sms_get_xml_int (cur_node, (xmlChar*)"w", 0);
                height = sms_get_xml_int (cur_node, (xmlChar*)"h", 0);

				val = xmlGetProp (cur_node, (xmlChar*)"icon");
				if(!val)
				   	return FALSE;
				sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
				printf("pic: %s\n", pic);
				pixbuf_page_up = gdk_pixbuf_new_from_file_at_size(pic, width, height, NULL);
				video_list_layout->page_controller.page_up = goo_canvas_image_new (layout->cavas, pixbuf_page_up,
						                                             video_list_layout->page_controller.page_up_x,
						                                             video_list_layout->page_controller.page_up_y, NULL);
				//g_object_set(video_list_layout->page_controller.page_up, "visibility", GOO_CANVAS_ITEM_HIDDEN, NULL);
			} else if(strcmp(cur_node->name, "page-text-field")==0) {
				video_list_layout->page_controller.text_x = sms_get_xml_double (cur_node, (xmlChar*)"x", 0);
				video_list_layout->page_controller.text_y = sms_get_xml_double (cur_node, (xmlChar*)"y", 0);
				width = sms_get_xml_int (cur_node, (xmlChar*)"w", 0);
				font = xmlGetProp (cur_node, (xmlChar*)"font");
				colour = xmlGetProp (cur_node, (xmlChar*)"colour");

				video_list_layout->page_controller.text = goo_canvas_text_new(layout->cavas, "", video_list_layout->page_controller.text_x,
						                                                      video_list_layout->page_controller.text_y,
						                                                      width, GTK_ANCHOR_NW,
						                                                      "font", font,
						                                                      "fill-color", colour,
						                                                      "ellipsize", PANGO_ELLIPSIZE_END,
						                                                      "alignment", PANGO_ALIGN_CENTER, NULL);
				//g_object_set(video_list_layout->page_controller.text, "visibility", GOO_CANVAS_ITEM_HIDDEN, NULL);

			} else if(strcmp(cur_node->name, "page-down-btn")==0) {
				video_list_layout->page_controller.page_down_x = sms_get_xml_double (cur_node, (xmlChar*)"x", 0);
				video_list_layout->page_controller.page_down_y = sms_get_xml_double (cur_node, (xmlChar*)"y", 0);
				width = sms_get_xml_int (cur_node, (xmlChar*)"w", 0);
				height = sms_get_xml_int (cur_node, (xmlChar*)"h", 0);

				val = xmlGetProp (cur_node, (xmlChar*)"icon");
				if(!val)
				   	return FALSE;
				sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
				printf("pic: %s\n", pic);
				pixbuf_page_down = gdk_pixbuf_new_from_file_at_size (pic, width, height, NULL);
				video_list_layout->page_controller.page_down = goo_canvas_image_new (layout->cavas, pixbuf_page_down,
										                                             video_list_layout->page_controller.page_down_x,
										                                             video_list_layout->page_controller.page_down_y, NULL);
				//g_object_set(video_list_layout->page_controller.page_down, "visibility", GOO_CANVAS_ITEM_HIDDEN, NULL);
			}
		}
	}

	g_object_unref(pixbuf_page_up);
	g_object_unref(pixbuf_page_down);
}

static SListIter *video_list_layout_create_left_list_iter(Video_List_Layout *video_list_layout, const char *text, GdkPixbuf *pixbuf_icon)
{
	printf("start func %s\n" ,__FUNCTION__);
	SListView *view=video_list_layout->left_list_view;
	SListIter *iter;
	
	iter=view->method->slistmethod_create_iter(view, "icon", pixbuf_icon, "text", text, NULL);	
	//g_object_unref(pixbuf_icon);
	return iter;
	
}

static SListIter *video_list_layout_create_right_list_iter(Video_List_Layout *video_list_layout, const char *text, GdkPixbuf *pixbuf_icon)
{
	printf("start func %s\n" ,__FUNCTION__);
	SListView *view=video_list_layout->right_list_view;
	SListIter *iter;

	iter=view->method->slistmethod_create_iter(view, "icon", pixbuf_icon, "text", text, NULL);
	//g_object_unref(pixbuf_icon);
	return iter;

}

static gboolean video_list_layout_load_list_resource(xmlNode *a_node, Layout *layout,
		                                         int *list_x, int *list_y,
		                                         int *item_border, int *item_height,
		                                         int *view_count, int *list_width,
		                                         GdkPixbuf **pixbuf_list_bg, GdkPixbuf **pixbuf_item_icon, GdkPixbuf **pixbuf_item_fg,
		                                         GdkPixbuf **pixbuf_down_btn, GdkPixbuf **pixbuf_up_btn)
{
	xmlChar *val;
	char pic[256];

	*list_x = sms_get_xml_int (a_node, (xmlChar*)"x", 0);
	*list_y = sms_get_xml_int (a_node, (xmlChar*)"y", 0);
	*item_border = sms_get_xml_int (a_node, (xmlChar*)"item_border", 0);
	*view_count = sms_get_xml_int (a_node, (xmlChar*)"view_count", 0);

	val = xmlGetProp (a_node, (xmlChar*)"list_bg");
	if(!val)
	   	return FALSE;
	sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
	printf("pic: %s\n", pic);
	*pixbuf_list_bg = gdk_pixbuf_new_from_file(pic, NULL);
	*list_width = gdk_pixbuf_get_width (*pixbuf_list_bg);

	val = xmlGetProp (a_node, (xmlChar*)"item_icon");
	if(!val)
	   	return FALSE;
	sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
	printf("pic: %s\n", pic);
	*pixbuf_item_icon = gdk_pixbuf_new_from_file(pic, NULL);

	val = xmlGetProp (a_node, (xmlChar*)"item_fg");
	if(!val)
	   	return FALSE;
	sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
	printf("pic: %s\n", pic);
	*pixbuf_item_fg = gdk_pixbuf_new_from_file(pic, NULL);
	*item_height = gdk_pixbuf_get_height (*pixbuf_item_fg);

	val = xmlGetProp (a_node, (xmlChar*)"down_btn");
	if(!val)
	  	return FALSE;
	sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
	printf("pic: %s\n", pic);
	*pixbuf_down_btn = gdk_pixbuf_new_from_file(pic, NULL);

	val = xmlGetProp (a_node, (xmlChar*)"up_btn");
	if(!val)
	   	return FALSE;
	sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
	printf("pic: %s\n", pic);
	*pixbuf_up_btn = gdk_pixbuf_new_from_file(pic, NULL);

	return TRUE;
}

static gboolean video_list_layout_load_page_num(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;

	video_list_layout->right_list_cur_page = 0;
	for (a_node; a_node; a_node = a_node->next) {
		if (a_node->type == XML_ELEMENT_NODE) {
			printf("node type: Element, name: %s\n", a_node->name);
			if(strcmp(a_node->name, "page")==0) {
				video_list_layout->right_list_total_page = sms_get_xml_int (a_node, (xmlChar*)"sum", 0);
				printf("total_page: %d\n", video_list_layout->right_list_total_page);
			}
		}
	}

	if (video_list_layout->right_list_total_page > 1) {
		video_list_layout_show_page_controller(layout, FALSE, TRUE, 1);
	} else if (video_list_layout->right_list_total_page == 1) {
		video_list_layout_show_page_controller(layout, FALSE, FALSE, 1);
	}

	return TRUE;
}

static gboolean video_list_layout_load_list_text(xmlNode *a_node, Layout *layout, int text_sum, int leftOrRight)
{
	printf("start func %s\n" ,__FUNCTION__);
	xmlNode *text_node = a_node;
	Video_List_Layout *video_list_layout = layout->private;
	xmlChar *val;

	int i = 0;
	for (text_node; text_node; text_node = text_node->next) {
			if (text_node->type == XML_ELEMENT_NODE) {
				printf("node type: Element, name: %s\n", text_node->name);
				if(strcmp(text_node->name, "item")==0) {
					if (leftOrRight == 0) {
						video_list_layout->left_list_text[i] = (List_Text *) g_malloc0(sizeof(List_Text) + 1);
						video_list_layout->left_list_text[i]->dir =  xmlGetProp (text_node, (xmlChar*)"dir");
						video_list_layout->left_list_text[i]->text = xmlGetProp (text_node, (xmlChar*)"value");
					    video_list_layout->left_list_text[i]->layout = xmlGetProp (text_node, (xmlChar*)"layout");
					    video_list_layout->left_list_text[i]->layout_xml_path = xmlGetProp (text_node, (xmlChar*)"layout_xml_path");
					    printf("text: %s, layout: %s \ndir: %s \nxml_path: %s \n", video_list_layout->left_list_text[i]->text,
							                                           video_list_layout->left_list_text[i]->layout,
							                                           video_list_layout->left_list_text[i]->dir,
							                                           video_list_layout->left_list_text[i]->layout_xml_path);
					} else if (leftOrRight == 1) {
						video_list_layout->right_list_text[i] = (List_Text *) g_malloc0(sizeof(List_Text) + 1);
						video_list_layout->right_list_text[i]->dir =  xmlGetProp (text_node, (xmlChar*)"dir");
						video_list_layout->right_list_text[i]->text = xmlGetProp (text_node, (xmlChar*)"value");
						video_list_layout->right_list_text[i]->layout = xmlGetProp (text_node, (xmlChar*)"layout");
						video_list_layout->right_list_text[i]->layout_xml_path = xmlGetProp (text_node, (xmlChar*)"layout_xml_path");
						printf("text: %s, layout: %s \n  dir: %s \nxml_path: %s \n", video_list_layout->right_list_text[i]->text,
								                                           video_list_layout->right_list_text[i]->layout,
								                                           video_list_layout->right_list_text[i]->dir,
								                                           video_list_layout->right_list_text[i]->layout_xml_path);
					}
					++i;
				}
			}
	}

	return TRUE;
}

static gboolean video_list_layout_load_list_by_page(Layout *layout, int left_list_item, int page, gboolean isReload)
{
	printf("start func %s\n" ,__FUNCTION__);

	char file_path[256];
	Video_List_Layout *video_list_layout = layout->private;

	if (isReload) {
		if (video_list_layout->left_list_text[left_list_item]->dir) {
			printf("++++++has dir\n");
			sprintf(file_path, "%s/%s/%s", TEACHER_VIDEO_XML_DIR, video_list_layout->left_list_text[left_list_item]->dir, "page_sum.xml");
	        video_list_layout_load_config(layout, file_path, &video_list_layout_load_page_num);
	        video_list_layout->right_list_cur_page = 1;
		} else {
			video_list_layout->right_list_total_page = 0;
			video_list_layout->right_list_cur_page = 0;
		}

	}

	//if (video_list_layout->left_list_text[left_list_item]->layout_xml_path) {
		sprintf(file_path, "%s/%s/%s%d%s", TEACHER_VIDEO_XML_DIR, video_list_layout->left_list_text[left_list_item]->dir, "page", page,".xml");
		printf("file_path: %s\n", file_path);
		video_list_layout_load_config(layout, file_path, &video_list_layout_load_right_list);
	//}

}

static gboolean video_list_layout_load_left_list(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);
	SMS *sms=layout->sms;
	Video_List_Layout *video_list_layout = layout->private;
	xmlNode *cur_node = a_node->children;
	//xmlChar *val;
	gboolean ret;
	int i;

	GdkPixbuf *pixbuf_list_bg, *pixbuf_item_icon, *pixbuf_item_fg, //*pixbuf_item_bg,
	          *pixbuf_down_btn, *pixbuf_up_btn;

	int list_x, list_y, item_border, view_count, list_width, item_height;

	ret = video_list_layout_load_list_resource(a_node, layout, &list_x, &list_y, &item_border, &item_height, &view_count, &list_width,
			                                       &pixbuf_list_bg, &pixbuf_item_icon, &pixbuf_item_fg,
				                                   &pixbuf_down_btn, &pixbuf_up_btn);



	for (cur_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			printf("node type: Element, name: %s\n", cur_node->name);
			if(strcmp(cur_node->name, "items")==0) {
			    printf("ready to load texts\n");
					video_list_layout->left_list_text_sum = sms_get_xml_int (cur_node, (xmlChar*)"sum", 0);
		       video_list_layout->left_list_text = (List_Text **) g_malloc0(sizeof(List_Text *)*video_list_layout->left_list_text_sum + 1);
		       video_list_layout_load_list_text(cur_node->children, layout, video_list_layout->left_list_text_sum, 0);
			}
		}
	}

	video_list_layout_load_list_by_page(layout, 0, 1, TRUE);

	if(!ret)
		return FALSE;

	video_list_layout->left_list_view = slistview_new(layout->cavas, list_x, list_y, list_width, item_height,
			                                                         item_border, view_count, sms->theme);
	slistview_set_pic(video_list_layout->left_list_view, pixbuf_list_bg, pixbuf_item_fg, pixbuf_up_btn, pixbuf_down_btn);
	
	for (i=0; i<video_list_layout->left_list_text_sum; i++)
	{
		slistview_append_iter(video_list_layout->left_list_view,
				video_list_layout_create_left_list_iter(video_list_layout, video_list_layout->left_list_text[i]->text, pixbuf_item_icon));
	}
	
	slistview_show(video_list_layout->left_list_view);
	video_list_layout->selected_list = 0;

	g_object_unref (G_OBJECT (pixbuf_list_bg));
	g_object_unref (G_OBJECT (pixbuf_item_fg));
	g_object_unref (G_OBJECT (pixbuf_up_btn));
	g_object_unref (G_OBJECT (pixbuf_down_btn));
	g_object_unref (G_OBJECT (pixbuf_item_icon));

	return TRUE;
}

static gboolean video_list_layout_load_right_list(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;
	SMS *sms=layout->sms;
	xmlNode *cur_node = NULL;
	gboolean ret;
	int i;

	GdkPixbuf *pixbuf_list_bg, *pixbuf_item_icon, *pixbuf_item_fg, //*pixbuf_item_bg,
	          *pixbuf_down_btn, *pixbuf_up_btn;

	int list_x, list_y, item_border, view_count, list_width, item_height;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				printf("node type: Element, name: %s\n", cur_node->name);
				if(strcmp(cur_node->name,"right-list")==0)
					break;
			}
	}

	ret = video_list_layout_load_list_resource(cur_node, layout, &list_x, &list_y, &item_border, &item_height, &view_count, &list_width,
				                                         &pixbuf_list_bg, &pixbuf_item_icon, &pixbuf_item_fg,
					                                     &pixbuf_down_btn, &pixbuf_up_btn);

	for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			printf("node type: Element, name: %s\n", cur_node->name);
			printf("ready to load right texts\n");
			video_list_layout->right_list_text_sum = sms_get_xml_int (cur_node, (xmlChar*)"sum", 0);
		    video_list_layout->right_list_text = (List_Text **) g_malloc0(sizeof(List_Text *)*video_list_layout->right_list_text_sum + 1);
		    ret &= video_list_layout_load_list_text(cur_node->children, layout, video_list_layout->right_list_text_sum, 1);
		}
	}


	if(!ret)
		return FALSE;

	video_list_layout->right_list_view = slistview_new(layout->cavas, list_x, list_y, list_width, item_height,
				                                                         item_border, view_count, sms->theme);
	slistview_set_pic(video_list_layout->right_list_view, pixbuf_list_bg, pixbuf_item_fg, pixbuf_up_btn, pixbuf_down_btn);

	for (i=0; i<video_list_layout->right_list_text_sum; i++)
	{
		slistview_append_iter(video_list_layout->right_list_view,
		          video_list_layout_create_right_list_iter(video_list_layout, video_list_layout->right_list_text[i]->text, pixbuf_item_icon));
	}

	slistview_show(video_list_layout->right_list_view);
	//video_list_layout->right_list_cur_page = 1;

	g_object_unref (G_OBJECT (pixbuf_list_bg));
	g_object_unref (G_OBJECT (pixbuf_item_fg));
	g_object_unref (G_OBJECT (pixbuf_up_btn));
	g_object_unref (G_OBJECT (pixbuf_down_btn));
	g_object_unref (G_OBJECT (pixbuf_item_icon));
	return TRUE;
}

static gboolean video_list_layout_travel_xml(xmlNode *a_node,Layout *layout) {
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;
	SMS *sms=layout->sms;
	xmlChar *val;
	char pic[256];
	xmlNode *cur_node = NULL;
	xmlNode *bg_node = NULL;
	xmlNode *animate_node = NULL;
	xmlNode *title_node = NULL;
	xmlNode *left_list_node = NULL;
	xmlNode *right_list_node = NULL;
	xmlNode *page_control_node = NULL;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			printf("node type: Element, name: %s\n", cur_node->name);
			if(strcmp(cur_node->name,"bg")==0)
				bg_node=cur_node;
			else if(strcmp(cur_node->name,"title")==0)
				title_node = cur_node;
			else if(strcmp(cur_node->name,"left-list")==0)
				left_list_node = cur_node;
			/*else if(strcmp(cur_node->name,"right-list")==0)
				right_list_node = cur_node;*/
			else if(strcmp(cur_node->name,"animates")==0)
				animate_node = cur_node;
		  else if(strcmp(cur_node->name,"page-control")==0) {
		    	page_control_node = cur_node;
		    }
		}
	}

		if(!bg_node||!animate_node||!title_node||!left_list_node||!page_control_node)
			return FALSE;

		video_list_layout_load_bg(bg_node,layout);
		video_list_layout_load_title(title_node, layout);
		video_list_layout_load_page_controller(page_control_node, layout);
		video_list_layout_load_left_list(left_list_node, layout);

		//video_list_layout_load_right_list(right_list_node, layout);
		//teacher_style_layout_load_animates (animate_node->children, layout);

	printf("finish func %s\n" ,__FUNCTION__);
	return TRUE;
}

static gboolean video_list_layout_load_config(Layout *layout, char *conf_file, gboolean (* travel_xml)(xmlNode *, Layout *)){
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;
	SMS *sms=layout->sms;

	gboolean ret=FALSE;
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;

/*	
	char conf[256];

	if (layout->layout_property.xml_path) {
		strcpy(conf, layout->layout_property.xml_path);
	} else {
		sprintf(conf, "%s/%s/layout/%s/layout.xml", SMS_RESOURCE_DIR, "default", layout->name);
		printf("conf: %s\n", conf);
	}
*/

	doc = xmlReadFile(conf_file, NULL, 0);

	if (doc == NULL) {
		fprintf(stderr,"error: could not parse file %s\n", conf_file);
		return ret;
	}

	root_element = xmlDocGetRootElement(doc);

	if(root_element){
		xmlNode *cur_node = NULL;
		for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				ret=TRUE;
				break;
			}
		}
		if(ret) {
			//ret=video_list_layout_travel_xml(cur_node->children,layout);
			ret = travel_xml(cur_node->children, layout);
		}
	}

	xmlFreeDoc(doc);
	xmlCleanupParser();
	wait=TRUE;
	printf("finish func %s\n" ,__FUNCTION__);
	return ret;
}

gboolean video_list_layout_init(Layout *layout,gpointer data)
{
	printf("start func %s\n" ,__FUNCTION__);
	printf("layout property xml path : %s\n", layout->layout_property.xml_path);

	SMS *sms=layout->sms;
	GdkPixbuf *pixbuf;
	char conf[256];

	Video_List_Layout *video_list_layout=(Video_List_Layout *)g_malloc0(sizeof(Video_List_Layout)+1);

	layout->private=(void *)video_list_layout;

	layout->cavas = goo_canvas_group_new(layout->parent,NULL);

	/*hjq add */
	if (layout->layout_property.xml_path) 
	{
		strcpy(conf, layout->layout_property.xml_path);
	}else
	{
		sprintf(conf, "%s/%s/layout/%s/layout.xml", SMS_RESOURCE_DIR, 
													"default", layout->name);
	}
	printf("conf: %s\n", conf);
	/**/

	if(!video_list_layout_load_config(layout, conf, &video_list_layout_travel_xml)){
		fprintf(stderr,"%s:load config err\n",__FUNCTION__);
		return FALSE;//exit(-1);
	}

	//video_list_layout->in_left=TRUE;

	printf("finish func %s\n" ,__FUNCTION__);
	return TRUE;
}

gboolean video_list_layout_clear(Layout *layout,gpointer data){

	return TRUE;
}

gboolean video_list_layout_key_press(Layout * layout,GdkEventKey *event) {
	printf("start func %s\n" ,__FUNCTION__);
	Video_List_Layout *video_list_layout = layout->private;

	if(wait) {
		if (event->keyval==GDK_Up){
			if(video_list_layout->selected_list == 0)
				slistview_scroll(video_list_layout->left_list_view, -1);
			else
				slistview_scroll(video_list_layout->right_list_view, -1);
			return TRUE;
		} else if (event->keyval==GDK_Down){
			if(video_list_layout->selected_list == 0)
				slistview_scroll(video_list_layout->left_list_view, 1);
			else
				slistview_scroll(video_list_layout->right_list_view, 1);
			return TRUE;
		} else if (event->keyval==GDK_Left){
			if(video_list_layout->selected_list == 1)
			{
				video_list_layout->selected_list = 0;
				slistview_set_active(video_list_layout->left_list_view, TRUE);
				slistview_set_active(video_list_layout->right_list_view, FALSE);
			}
			return TRUE;
		} else if (event->keyval==GDK_Right){
			if(video_list_layout->selected_list == 0)
			{
				video_list_layout->selected_list = 1;
				slistview_set_active(video_list_layout->left_list_view, FALSE);
				slistview_set_active(video_list_layout->right_list_view, TRUE);
			}
			return TRUE;
		} else if (event->keyval == GDK_Return){
			if(video_list_layout->selected_list == 0) {
				printf("video_list_layout: cur left list num %d\n", video_list_layout->left_list_view->list_cur);

				//video_list_layout_load_list_by_page(Layout *layout, int left_list_item, int page, gboolean isReload)

				int cur_list_item = video_list_layout->left_list_view->list_cur + video_list_layout->left_list_view->view_cur;
				video_list_layout_remove_right_list(layout);
				video_list_layout_load_list_by_page(layout, cur_list_item, 1, TRUE);
				printf("cur page: %d\n", video_list_layout->right_list_cur_page);

				/*char *right_list_xml = video_list_layout->left_list_text[cur_list_item]->layout_xml_path;
				if(right_list_xml) {
					video_list_layout_remove_right_list(layout);
				  	video_list_layout_load_config(layout, right_list_xml, &video_list_layout_load_right_list);
				}*/
			} else {

			}
            return TRUE;
		} else if (event->keyval==GDK_Page_Up){
			printf("video_list_layout: cur left view num %d\n", video_list_layout->left_list_view->list_cur +video_list_layout->left_list_view->view_cur);
			printf("total page: %d\n", video_list_layout->right_list_total_page);
			printf("cur page: %d\n", video_list_layout->right_list_cur_page);
			if (video_list_layout->right_list_cur_page > 1
					&& video_list_layout->right_list_cur_page <= video_list_layout->right_list_total_page) {
				video_list_layout_remove_right_list(layout);
				video_list_layout->right_list_cur_page -= 1;
				video_list_layout_load_list_by_page(layout,
						              video_list_layout->left_list_view->list_cur + video_list_layout->left_list_view->view_cur,
						              video_list_layout->right_list_cur_page, FALSE);
				if (video_list_layout->right_list_cur_page == 1) {
					video_list_layout_show_page_controller(layout, FALSE, TRUE, video_list_layout->right_list_cur_page);
				} else
					video_list_layout_show_page_controller(layout, TRUE, TRUE, video_list_layout->right_list_cur_page);
			}
			return TRUE;
		} else if (event->keyval==GDK_Page_Down){
			printf("video_list_layout: cur left view num %d\n", video_list_layout->left_list_view->list_cur +video_list_layout->left_list_view->view_cur);
			printf("total page: %d\n", video_list_layout->right_list_total_page);
			printf("cur page: %d\n", video_list_layout->right_list_cur_page);
			if (video_list_layout->right_list_cur_page >= 1
					&& video_list_layout->right_list_cur_page < video_list_layout->right_list_total_page) {
				video_list_layout_remove_right_list(layout);
				video_list_layout->right_list_cur_page += 1;
				video_list_layout_load_list_by_page(layout,
									  video_list_layout->left_list_view->list_cur + video_list_layout->left_list_view->view_cur,
									  video_list_layout->right_list_cur_page, FALSE);
				printf("cur page: %d\n", video_list_layout->right_list_cur_page);

				if (video_list_layout->right_list_cur_page == video_list_layout->right_list_total_page) {
					video_list_layout_show_page_controller(layout, TRUE, FALSE, video_list_layout->right_list_cur_page);
				} else
					video_list_layout_show_page_controller(layout, TRUE, TRUE, video_list_layout->right_list_cur_page);
			}
			return TRUE;
		}
	}
	return FALSE;
}

Layout *video_list_layout_new(GooCanvasItem * parent,gpointer sms){
	Layout *layout=layout_new(parent, LAYOUT_TYPE_WIN, LAYOUT_WIN_VIDEO_LIST, 
																								VIDEO_LIST_LAYOUT_NAME, sms);
	layout_set_func(layout,"init", video_list_layout_init);
	layout_set_func(layout,"clear", video_list_layout_clear);
	layout_set_func(layout,"key-press", video_list_layout_key_press);

	return layout;
}
